const express = require('express');
const {Article} = require('../model/article');
const {Comment} = require('../model/comment');
const pagination = require('mongoose-sex-page');
// 创建博客展示页面路由
const home = express.Router();
home.get('/',async (req,res)=>{
    const page = req.query.page;
    let result = await pagination(Article).page(page).size(4).display(5).find().populate('author').exec();
    // res.send(result);
    res.render('home/default',{
        result:result
    });
});

// 博客前台文章详情展示页面
home.get('/article',async (req,res)=>{
    const id = req.query.id;
    let article = await Article.findById(id).populate('author');
    // 查询当前文章评论信息
    let comments = await Comment.find({aid:id}).populate('uid');
    res.render('home/article.art',{
        article,comments
    });
});

// 评论功能路由
home.post('/comment',async (req,res)=>{
    const {content,uid,aid} = req.body;
    await Comment.create({
        content:content,
        aid:aid,
        uid:uid,
        time:new Date
    });
    res.redirect('/home/article?id='+aid);
});
module.exports = home;